package com.rickpan.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 团队实体类
 */
@Entity
@Table(name = "teams")
@EntityListeners(AuditingEntityListener.class)
public class Team {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 100)
    private String name;

    @Column(columnDefinition = "TEXT")
    private String description;

    @Column(name = "creator_id", nullable = false)
    private Long creatorId;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private Status status = Status.PENDING;

    @Column(name = "max_members", nullable = false)
    private Integer maxMembers = 50; // VIP用户最多50人

    @Column(name = "current_members", nullable = false)
    private Integer currentMembers = 1; // 创建者默认为成员

    @Column(name = "team_avatar_url", length = 500)
    private String teamAvatarUrl;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private Visibility visibility = Visibility.INTERNAL;

    @Enumerated(EnumType.STRING)
    @Column(name = "join_method", nullable = false)
    private JoinMethod joinMethod = JoinMethod.INVITE_ONLY;

    @Column(name = "require_two_factor", nullable = false)
    private Boolean requireTwoFactor = false;

    @Column(name = "session_timeout", nullable = false)
    private Integer sessionTimeout = 24;

    @Column(name = "team_settings", columnDefinition = "JSON")
    private String teamSettings;

    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    // 团队成员关系（一对多）
    @OneToMany(mappedBy = "team", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<TeamMember> members = new ArrayList<>();

    // 团队项目关系（一对多）
    @OneToMany(mappedBy = "team", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<Project> projects = new ArrayList<>();

    // 枚举定义
    public enum Status {
        PENDING,    // 待审核
        APPROVED,   // 已批准
        REJECTED,   // 已拒绝
        ARCHIVED    // 已归档
    }

    public enum Visibility {
        PUBLIC,     // 公开 - 所有用户可见
        INTERNAL,   // 内部 - 登录用户可见
        PRIVATE     // 私有 - 仅成员可见
    }

    public enum JoinMethod {
        INVITE_ONLY,    // 仅邀请
        REQUEST,        // 申请加入
        OPEN           // 开放加入
    }

    // 构造函数
    public Team() {}

    public Team(String name, String description, Long creatorId) {
        this.name = name;
        this.description = description;
        this.creatorId = creatorId;
    }

    // Getter 和 Setter 方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(Long creatorId) {
        this.creatorId = creatorId;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Integer getMaxMembers() {
        return maxMembers;
    }

    public void setMaxMembers(Integer maxMembers) {
        this.maxMembers = maxMembers;
    }

    public Integer getCurrentMembers() {
        return currentMembers;
    }

    public void setCurrentMembers(Integer currentMembers) {
        this.currentMembers = currentMembers;
    }

    public String getTeamAvatarUrl() {
        return teamAvatarUrl;
    }

    public void setTeamAvatarUrl(String teamAvatarUrl) {
        this.teamAvatarUrl = teamAvatarUrl;
    }

    public Visibility getVisibility() {
        return visibility;
    }

    public void setVisibility(Visibility visibility) {
        this.visibility = visibility;
    }

    public JoinMethod getJoinMethod() {
        return joinMethod;
    }

    public void setJoinMethod(JoinMethod joinMethod) {
        this.joinMethod = joinMethod;
    }

    public Boolean getRequireTwoFactor() {
        return requireTwoFactor;
    }

    public void setRequireTwoFactor(Boolean requireTwoFactor) {
        this.requireTwoFactor = requireTwoFactor;
    }

    public Integer getSessionTimeout() {
        return sessionTimeout;
    }

    public void setSessionTimeout(Integer sessionTimeout) {
        this.sessionTimeout = sessionTimeout;
    }

    public String getTeamSettings() {
        return teamSettings;
    }

    public void setTeamSettings(String teamSettings) {
        this.teamSettings = teamSettings;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public List<TeamMember> getMembers() {
        return members;
    }

    public void setMembers(List<TeamMember> members) {
        this.members = members;
    }

    public List<Project> getProjects() {
        return projects;
    }

    public void setProjects(List<Project> projects) {
        this.projects = projects;
    }

    /**
     * 检查团队是否已满员
     */
    public boolean isFull() {
        return currentMembers >= maxMembers;
    }

    /**
     * 检查团队是否已激活
     */
    public boolean isActive() {
        return status == Status.APPROVED;
    }

    @Override
    public String toString() {
        return "Team{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", creatorId=" + creatorId +
                ", status=" + status +
                ", maxMembers=" + maxMembers +
                ", currentMembers=" + currentMembers +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }
}
